package com.intelligent.ispc.foundation.event;

import com.google.common.collect.Maps;
import com.google.common.eventbus.EventBus;

import java.util.Map;

/**
 * Created by Peter on 2016/4/22.
 */
public class ApplicationEventBus {
    private static final EventBus instance = new EventBus();
    private static Map<String, Listener> listenerMap = Maps.newConcurrentMap();

    public static EventBus getEventBus() {
        return instance;
    }

    public static void registerListener(Listener listener) {
        instance.register(listener);
        listenerMap.put(listener.getName(), listener);
    }

    public static void unregisterListener(Listener listener) {
        instance.unregister(listener);
        if (listenerMap.containsKey(listener.getName())) {
            listenerMap.remove(listener.getName());
        }
    }

    public static void post(Event event) {
        instance.post(event);
    }

    public static Listener getListener(String name) {
        if (listenerMap.containsKey(name)) {
            return listenerMap.get(name);
        }

        return null;
    }
}
